import pandas as pd, xlwings as xw
path=r"E:\韦瑞奎\E1其他工作\财务部\舒小红/合并同类项目.xlsx"
df=pd.read_excel(path,"合并同类项目")
print(df)
# 将日期字符串转换为datetime
# df['Date'] = pd.to_datetime(df['Payment Date'])
df=df.fillna("")
df["合并项目"]=df['INVOICE NO']+df['Payment Date']
dfs=df.drop_duplicates(subset="合并项目")
# 使用groupby按ID合并日期
df_merged = dfs.groupby('INVOICE NO')['Payment Date'].agg(lambda x: ', '.join(x.astype(str))).reset_index()
print(df_merged)
wb = xw.Book(path)
sheet0 = wb.sheets['合并同类项']
sheet0.range('A1').value = df_merged
返回值:
INVOICE | NO | PaymentDate |
0 | JCHK20201101 | 2021-02-05 |
1 | JCHK20201102 | 2021-02-19 |
2 | JCHK20201103 | 2021-02-19 |
... | ... | ... |
30907 | JCHK20221012 | 2022-11-25 |
30908 | JCHK20221012 | 2023-03-27 |
30909 | JVM240102401 | 1900-01-00 |
INVOICE | NO | PaymentDate |
637 | JCHK20221012 | 2022-11-25,2023-03-2 |
639 | JCHK20221014 | 2022-11-30,2022-11-29 |
645 | JCHK20221020 | 2022-11-30,2022-12-02,2023-03-24 |
646 | JCHK20221021 | 2022-12-02,2023-03-27 |
652 | JCHK20221106 | 2022-12-12,2022-12-14,2022-12-23 |